package com.sevlet;

import com.Bean.*;
import com.dao.StatisticsDao;
import com.dao.StatisticsThreeDao;
import com.dao.StatisticsTwoDao;
import com.dao.StatisticsfourDao;
import com.dao.impl.StatisticsDaoThreelmpl;
import com.dao.impl.StatisticsDaoTwolmpl;
import com.dao.impl.StatisticsDaofourlmpl;
import com.dao.impl.StatisticsDaolmpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JdbcUtil;
import com.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/IndexSevlet")
public class IndexSevlet extends BaseSevlet {
    
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        IndexBean indexBean = new IndexBean();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        Fddate fddate=new Fddate();

        String[] times = req.getParameterValues("times[]");

      if (times!=null){

          fddate.setFristDay(times[0]);
          fddate.setLastDay(times[1]);
      }

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String sql1="select count(*) inNumber  from room_list GROUP BY intime having TO_DAYS(intime) = TO_DAYS(NOW())";
            Double inNumber = JdbcUtil.queryRooms(sql1);
            indexBean.setInNumber(inNumber);

            String sql2="select count(*) outNumber from room_list GROUP BY outtime having TO_DAYS(outtime) = TO_DAYS(NOW())";
            Double outNumber = JdbcUtil.queryRooms(sql2);
            indexBean.setOutNumber(outNumber);

            String sql3="select count(*) newNumber from room_list where TO_DAYS(ldate) = TO_DAYS(NOW())";
            Double newNumber = JdbcUtil.queryRooms(sql3);
            indexBean.setNewNumber(newNumber);
//今天所有的房价
            String sql4="SELECT IF( ( SELECT date_format( CURDATE( ), '%w' ) FROM DUAL ) = 5|6,sum(wkprice),sum(weprice)\n" +
                    ") allRoomPrice from room_id,room where room.rtid=room_id.rtid";
            Double allRoomPrice = JdbcUtil.queryRooms(sql4);
            indexBean.setAllRoomPrice(allRoomPrice);

            String sql5="select count(*) rooms from room_id";
            Double rooms = JdbcUtil.queryRooms(sql5);
            indexBean.setRooms(rooms);

            String sql6= "SELECT intime , count(*) count from room_list  GROUP BY intime  " +
                    "HAVING DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(intime) ORDER BY intime";

            List<IndexBean> indexBeans = JdbcUtil.queryData(sql6, IndexBean.class);
            System.out.println(indexBeans.toString());
            indexBean.setData(indexBeans);



            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(indexBean);

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            out.print(gson.toJson(jsonUtil));
        }

    }


    public void querytwo (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BeizhuBean beizhuBean = new BeizhuBean();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        Fddate fddate=new Fddate();

        String[] times = req.getParameterValues("times[]");

        if (times!=null){

            fddate.setFristDay(times[0]);
            fddate.setLastDay(times[1]);
        }

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {


            String sql= "select intime,rname,room_list.rid ,pname from room_list,room_id,room,client where room_list.rid=room_id.rid and room_id.rtid=room.rtid and room_list.pno=client.pno\n" +
                    "and TO_DAYS(intime)-TO_DAYS(NOW()) in (0,1) ORDER BY intime";

            List<BeizhuBean> beizhuBeans = JdbcUtil.queryData(sql, BeizhuBean.class);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(beizhuBeans);

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            out.print(gson.toJson(jsonUtil));
        }

    }
    }
